package com.jlsoft.config.oauth;

import com.jlsoft.config.oauth.handler.BrowerAuthenticationFailHandler;
import com.jlsoft.config.oauth.handler.BrowerAuthenticationSuccessHandler;
import com.jlsoft.config.oauth.handler.BrowerLogoutSuccesHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

/**
 * 资源服务器
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

    @Autowired
    private BrowerAuthenticationSuccessHandler successHandler;

    @Autowired
    private BrowerAuthenticationFailHandler failHandler;

    @Autowired
    private BrowerLogoutSuccesHandler browerLogoutSuccesHandler;

    @Override
    public void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
               // .antMatchers("/jwtme").permitAll()
                .anyRequest()
                //.access("@rbacService.hasPermission(request, authentication)")
                .authenticated()

                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/authentication/form")
                .successHandler(successHandler)
                .failureHandler(failHandler)
                .and()

                .logout()
                .logoutUrl("/signOut")
                .logoutSuccessHandler(browerLogoutSuccesHandler)
                .permitAll()
                .and()

                .csrf().disable();

    }
}